package io.renren.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.IntDexUtils;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.dao.EslDao;
import io.renren.dao.RoomEslDao;
import io.renren.dao.SeatEslDao;
import io.renren.entity.EslEntity;
import io.renren.entity.RoomEslEntity;
import io.renren.entity.SeatEslEntity;
import io.renren.param.EslHeartBeatData;
import io.renren.param.EslHeartBeatParam;
import io.renren.service.EslService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


@Slf4j
@Service("eslService")
public class EslServiceImpl extends ServiceImpl<EslDao, EslEntity> implements EslService {

    @Autowired
    private EslDao eslDao;

    @Autowired
    private RoomEslDao roomEslDao;

    @Autowired
    private SeatEslDao seatEslDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String eslId = (String) params.get("eslId");
        String user = (String) params.get("user");
        String apId = (String) params.get("apId");
        List<String> eslIds = (List<String>) params.get("eslIds");

        IPage<EslEntity> page = this.page(
                new Query<EslEntity>().getPage(params),
                new QueryWrapper<EslEntity>()
                        .eq(eslId != null, "esl_id", eslId)
                        .eq(user != null, "user", user)
                        .eq(apId != null, "ap_id", apId)
                        .in(eslIds != null && eslIds.size() > 0, "esl_id", eslIds)
        );

        return new PageUtils(page);
    }

    public EslEntity queryByEslId(String eslId) {
        return eslDao.selectOne(new QueryWrapper<EslEntity>().eq("esl_id", eslId));
    }

    public boolean batchSave(EslHeartBeatParam param) {
        // esl heart beat
        /*{
            "status_no": "0",
                "type": "ESL_HB_STATUS",
                "user": "aaaa",
                "data": [{
                    "ap_id": "2",
                    "last_hb_time": "0",
                    "firmware_id": "",
                    "rf_power": "192.168.9.253",
                    "reserve": "NONE",
                    "set_wor": "2",
                    "nfc": "standby",
                    "esl_id": "DC:07:C1:01:01:4C",
                    "battery": "14",
                    "user": "NONE",
                    "rom_version": ""
        }]
        }*/

        List<EslEntity> eslEntityList = new ArrayList<>();
        for (EslHeartBeatData datum : param.getData()) {


            EslEntity eslEntity = queryByEslId(datum.getEslId());

            if (eslEntity != null && eslEntity.getEslId().equals(datum.getEslId())) {
                log.debug("eslId = {} 已存在，更新", datum.getEslId());
                //esl 已存在，更新
                Long id = eslEntity.getId();
                eslEntity = datum.convertFor(datum);
                eslEntity.setId(id);
                updateById(eslEntity);
            } else {
                log.debug("eslId = {} 不存在，新增", datum.getEslId());
                //不存在，新增
                eslEntity = datum.convertFor(datum);
                eslEntity.setStatusNo(param.getStatusNo());
                eslEntity.setType(param.getType());
                eslEntity.setUser(param.getUser());
                eslEntityList.add(eslEntity);
            }
        }
        saveBatch(eslEntityList);
        return true;
    }



}
